using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Services;
using BLL;
namespace mmq
{

    public partial class Reviews : BasePage
    {
        private readonly int _startRowIndex = 1;
        private readonly int _maximumRows = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            string UserCulture = HttpContext.Current.Profile.GetPropertyValue("PreferredCulture").ToString();

            IDataReader reader = BLL.ReviewManager.GetRandomReview(UserCulture);
            if (reader.Read())
            {
                MetaKeyword = reader["Title"].ToString();
                Description = reader["Review"].ToString();
            }

            if (!IsPostBack)
            {
                TreeEditor1.SelectAction = TreeNodeSelectAction.Select;
                TreeEditor2.SelectAction = TreeNodeSelectAction.Expand;
                TreeEditor2.NavigateUrl = @"javascript:OnNodeClick('{0}','{1}','" + label_category.ClientID + "','" + hf_selectedValue.ClientID + "','" + btn_Ok.ClientID + "'); ";
                bindRepeaters(-1, _startRowIndex, _maximumRows,true);
            }
        }
        private void bindRepeaters(int selectedId, int startRowIndex, int maximumRows,bool bindPage)
        {
            DataSet ds = BLL.ReviewManager.GetAllReviews(selectedId, HttpContext.Current.Profile.GetPropertyValue("PreferredCulture").ToString(), startRowIndex, maximumRows);
            MainPage.DataSource = ds.Tables["Category"];
            if (bindPage)
            {
                rptrPage.DataSource = GetPageTable(int.Parse(ds.Tables["RowInfo"].Rows[0]["TotalRows"].ToString()));
            }
            Page.DataBind();

        }
        protected void btn_Ok_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(HtmlEditor1.Text) && !string.IsNullOrEmpty(txtBoxTitle.Text))
            {
                string sculture = HttpContext.Current.Profile.GetPropertyValue("PreferredCulture").ToString();
                if (string.IsNullOrEmpty(sculture))
                    sculture = "zh-CN";
                string suserName = HttpContext.Current.User.Identity.Name;
                if (string.IsNullOrEmpty(suserName))
                    suserName = "visitor";

                BLL.ReviewManager.AddReview(Convert.ToInt32(hf_selectedValue.Value), suserName, sculture, txtBoxTitle.Text, HtmlEditor1.Text, ajaxRate.CurrentRating);
            }
        }

        protected void reviews_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "Delete")
            {
                ReviewManager.RemoveReview(id);
            }
            if (e.CommandName == "Delete" || e.CommandName == "Select")
            {
                ViewState["CurrentSelection"] = id;
                bindRepeaters(id, _startRowIndex, _maximumRows,true);
            }
            if (e.CommandName == "SelectPage")
            {
                int parentid = -1;
                if (ViewState["CurrentSelection"] != null)
                    parentid = (int)ViewState["CurrentSelection"];
                bindRepeaters(parentid, (id-1) * _maximumRows +1, _maximumRows, true);
            }


        }

        protected void reviews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                LinkButton lb = e.Item.FindControl("lb_remove") as LinkButton;
                lb.CommandArgument = DataBinder.Eval(e.Item.DataItem, "[\"ID\"]").ToString();
            }
        }
        protected void categorySelected(object sender, EventArgs e)
        {
            int selectedId = -1;
            if (!string.IsNullOrEmpty(TreeEditor1.SelectedValue))
                selectedId = int.Parse(TreeEditor1.SelectedValue);
            bindRepeaters(selectedId, _startRowIndex, _maximumRows,true);
        }
        private DataTable GetPageTable(int totalRows)
        {
            int pageCount =(int) totalRows / _maximumRows + 1;
            if (pageCount < 2)
                return null;
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn();
            dc.ColumnName = "pagecount";
            dc.DataType = typeof(int);
            dt.Columns.Add(dc);
            for(int i=0;i<pageCount;i++)
            {
                DataRow dr = dt.NewRow();
                dr["pagecount"] = i + 1;
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
